/*
 * @Author: taoyuqing
 * @Date: 2022-12-22 00:59:06
 * @Description: 轮播图
 */
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';

import '../utils/utils.dart';

class BannerSlider extends StatefulWidget {
  BannerSlider({Key? key}) : super(key: key);

  @override
  State<BannerSlider> createState() => _BannerSliderState();
}

class _BannerSliderState extends State<BannerSlider> with AutomaticKeepAliveClientMixin{
  final List<String> imageList = ['11', '12', '12', '12', '12'];
  int _current = 0;
  // final CarouselController _controller = CarouselController();
  @override
  Widget build(BuildContext context) {
    final double WIDTH = Utils().getScreenWidth(context) - 40;
    return Column(
      children: [
        CarouselSlider(
          options: CarouselOptions(
              height: WIDTH * 0.45,
              autoPlay: true,
              viewportFraction: 1,
              onPageChanged: (index, reason) => {
                    setState(() {
                      _current = index;
                    })
                  }),
          items: imageList.map((i) {
            return Builder(
              builder: (BuildContext context) {
                return Container(
                  width: WIDTH,
                  child: Image.asset(
                    'images/mob-banner.png',
                    fit: BoxFit.fill,
                  ),
                );
              },
            );
          }).toList(),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: imageList.asMap().keys.map((entry) {
            return GestureDetector(
              onTap: () {},
              child: Container(
                width: _current == entry ? 24 : 8.0,
                height: 8.0,
                margin:
                    const EdgeInsets.symmetric(vertical: 8.0, horizontal: 4.0),
                decoration: BoxDecoration(
                    borderRadius: const BorderRadius.all(Radius.circular(4.0)),
                    color: _current == entry
                        ? Colors.yellow
                        : const Color(0x33ffffff)),
              ),
            );
          }).toList(),
        )
      ],
    );
  }
  
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

}
